package org.makcro.sms.services.chat.model;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name = "subscriber")
@NamedQueries({ @NamedQuery(name = "Subscriber.findByPhonenumberAndState", query = "SELECT s FROM Subscriber s WHERE s.phonenumber = :phonenumber AND s.unsubscribed = :unsubscribed"),
		@NamedQuery(name = "Subscriber.updateExpired", query = "UPDATE Subscriber s SET s.unsubscribed = true WHERE s.subscriptionDate < :lastMonth"),
		@NamedQuery(name = "Subscriber.findByPhonenumber", query = "SELECT s FROM Subscriber s WHERE s.phonenumber = :phonenumber"),
		@NamedQuery(name = "Subscriber.unsubscribeByPhonenumber", query = "UPDATE Subscriber s SET s.unsubscribed = true WHERE s.phonenumber = :phonenumber")

})
public class Subscriber implements Serializable{

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "Id")
	private int id;

	@Column(name = "Phonenumber")
	private String phonenumber;

	@Column(name = "Note")
	private String note;

	@Column(name = "SubscriptionDate")
	@Temporal(TemporalType.TIMESTAMP)
	private Date subscriptionDate;

	@Column(name = "Unsubscribed")
	private boolean unsubscribed;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getPhonenumber() {
		return phonenumber;
	}

	public void setPhonenumber(String phonenumber) {
		this.phonenumber = phonenumber;
	}

	public String getNote() {
		return note;
	}

	public void setNote(String note) {
		this.note = note;
	}

	@Override
	public int hashCode() {
		return id;
	}

	public Date getSubscriptionDate() {
		return subscriptionDate;
	}

	public void setSubscriptionDate(Date subscriptionDate) {
		this.subscriptionDate = subscriptionDate;
	}

	public boolean isUnsubscribed() {
		return unsubscribed;
	}

	public void setUnsubscribed(boolean unsubscribed) {
		this.unsubscribed = unsubscribed;
	}

	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof Subscriber)) {
			return false;
		}

		Subscriber other = (Subscriber) obj;
		if (this.id != other.id) {
			return false;
		}
		return true;
	}

}
